package net.flyinggroup.gateway.controller;

import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

@RestController
public class FaveCarsController {
    private final WebClient.Builder carClient;

    public FaveCarsController(WebClient.Builder carClient) {
        this.carClient = carClient;
    }

    @GetMapping("/fave-cars")
    public Flux<Car> faveCars() {
        return carClient.build()
                .get()
                .uri("lb://car/cars")
                .retrieve()
                .bodyToFlux(Car.class)
                .filter(this::isFavorite);
    }

    @GetMapping("/car-fallback")
    public Flux<Car> noCars() {
        return Flux.empty();
    }

    private boolean isFavorite(Car car) {
        return car.getName().equals("ID. BUZZ");
    }
}

@Data
class Car {
    private String name;
    private LocalDate releaseDate;
}
